//
//  NSArray+JSON.m
//  UtilsDemo
//
//  Created by yunsheng on 15/4/22.
//  Copyright (c) 2015年 yunsheng. All rights reserved.
//

#import "NSArray+JSON.h"

#import "NSDictionary+JSON.h"

@implementation NSArray (JSON)

- (NSString *)toJsonStrCascade
{
    NSString *jsonString = @"";
    for (int i = 0; i < self.count; i++) {
        if ([[self objectAtIndex:i] isKindOfClass:[NSString class]]) {
            if (i == self.count - 1) {
                jsonString = [NSString stringWithFormat:@"%@\"%@\"", jsonString, [self objectAtIndex:i]];
            } else {
                jsonString = [NSString stringWithFormat:@"%@\"%@\",", jsonString, [self objectAtIndex:i]];
            }
        } else if([[self objectAtIndex:i] isKindOfClass:[NSDictionary class]] || [[self objectAtIndex:i] isKindOfClass:[NSArray class]]) {
            if (i == self.count - 1) {
                jsonString = [NSString stringWithFormat:@"%@%@", jsonString, [[self objectAtIndex:i] toJsonStrCascade]];
            } else {
                jsonString = [NSString stringWithFormat:@"%@%@,", jsonString, [[self objectAtIndex:i] toJsonStrCascade]];
            }
        } else {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\"",jsonString, [self objectAtIndex:i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\"%@\",",jsonString, [self objectAtIndex:i]];
            }
        }
    }
    
    return [NSString stringWithFormat:@"[%@]", jsonString];
}

-(NSString *)toStrOfJsonStrCascade
{
    NSString *jsonString = @"";
    for (int i = 0; i < self.count; i++) {
        if ([[self objectAtIndex:i] isKindOfClass:[NSString class]]) {
            if (i == self.count - 1) {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\"", jsonString, [self objectAtIndex:i]];
            } else {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\",", jsonString, [self objectAtIndex:i]];
            }
        } else if([[self objectAtIndex:i] isKindOfClass:[NSDictionary class]] || [[self objectAtIndex:i] isKindOfClass:[NSArray class]]) {
            if (i == self.count - 1) {
                jsonString = [NSString stringWithFormat:@"%@%@", jsonString, [[self objectAtIndex:i] toStrOfJsonStrCascade]];
            } else {
                jsonString = [NSString stringWithFormat:@"%@%@,", jsonString, [[self objectAtIndex:i] toStrOfJsonStrCascade]];
            }
        } else {
            if (i == self.count - 1)
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\"",jsonString, [self objectAtIndex:i]];
            }
            else
            {
                jsonString = [NSString stringWithFormat:@"%@\\\"%@\\\",",jsonString, [self objectAtIndex:i]];
            }
        }
    }
    
    return [NSString stringWithFormat:@"[%@]", jsonString];
}

@end
